﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ProjectSemIII.Entity;

namespace ProjectSemIII.Process
{
    public class UserProcess
    {
        private project_sem3Entities projectEntity;
        private Generator gene;
        public UserProcess()
        {
            projectEntity = new project_sem3Entities();
            gene = new Generator();
        }
        public bool checkUserNameExits(string username)
        {
            bool doesUserExist = projectEntity.Users.Any(u => u.Username == username);
            return doesUserExist;
        }
        public bool checkRollNoExits(string rollNo)
        {
            bool doesUserExist = projectEntity.Users.Any(u => u.RollNo == rollNo);
            return doesUserExist;
        }
        public int createNewUser(User user)
        {
            try
            {
                projectEntity.Users.Add(user);
                projectEntity.SaveChanges();
                return 100;
            }
            catch
            {
                return 100;
            }

        }

        public User loginProcess(string userName, string inputPassword)
        {
            User u = new User();
            try
            {
                u = (from a in projectEntity.Users
                     where a.Username == userName
                     select a).First();
                string md5Password = gene.CreateMD5Hash(inputPassword);
                string passwordMd5 = md5Password + u.Salt;
                string password = gene.CreateMD5Hash(passwordMd5);
                if (password == u.Password)
                {
                    return u;
                }
                else
                {
                    u = new User();
                    return u;
                }
            }
            catch
            {
                return u;
            }
        }

        public int checkUserNamePassword(string userName, string inputPassword)
        {
            User u = new User();
            try
            {
                if (checkUserNameExits(userName) == false)
                {
                    return 100;
                }
                else
                {
                    u = (from a in projectEntity.Users
                         where a.Username == userName
                         select a).Single();
                    string md5Password = gene.CreateMD5Hash(inputPassword);
                    string passwordMd5 = md5Password + u.Salt;
                    string password = gene.CreateMD5Hash(passwordMd5);
                    if (password != u.Password)
                    {
                        return 90;
                    }
                    else
                    {
                        return 10;
                    }
                }
            }
            catch
            {
                return 80;
            }
        }
    }
}